package com.ssmk.module.merchant.view;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.liaoinstan.springview.widget.SpringView;
import com.ssmk.R;
import com.ssmk.adapter.CommodityTypeAdapter;
import com.ssmk.adapter.ShoppingAdaoter;
import com.ssmk.base.MVPBaseActivity;
import com.ssmk.module.home.utils.FontHelper;
import com.ssmk.module.merchant.Presenter.MerShoppingPresenter;
import com.ssmk.module.merchant.Presenter.PopwinPrestenThree;
import com.ssmk.module.merchant.Presenter.PopwindPresenter;
import com.ssmk.module.merchant.Presenter.PopwindPresetertwo;
import com.ssmk.module.merchant.Presenter.PopwindPresterFour;
import com.ssmk.module.merchant.Presenter.ShangpinPrestenser;
import com.ssmk.module.merchant.bean.GoodsBean;
import com.ssmk.module.merchant.bean.PopBeans;
import com.ssmk.module.merchant.bean.PopowindBean;
import com.ssmk.module.merchant.bean.PopwinTwoBean;
import com.ssmk.module.merchant.bean.PopwindFourBean;
import com.ssmk.module.merchant.bean.PopwindThreeBean;
import com.ssmk.module.merchant.bean.ShangpinBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：${CaiJianNan}
 * 时间2017/6/28 14:22
 * 邮箱：17600157003@163.com
 */
public class Shopping_Activity extends MVPBaseActivity  {
    @BindView(R.id.shopping_finsh)
    ImageView mShoppingFinsh;
    @BindView(R.id.shopping_edit)
    EditText mShoppingEdit;
    @BindView(R.id.shopping_menu)
    ImageView mShoppingMenu;
    @BindView(R.id.shopping_recycler)
    RecyclerView mShoppingRecycler;
    @BindView(R.id.shipping_card)
    CardView shippingCard;
    @BindView(R.id.textinput)
    TextView textinput;
    @BindView(R.id.shopping_zonghe)
    Button shoppingZonghe;
    @BindView(R.id.shopping_price)
    Button shoppingPrice;
    @BindView(R.id.shopping_xiaoliang)
    Button shoppingXiaoliang;
    @BindView(R.id.shipping_spview)
    SpringView shoppingSpview;


    private PopupWindow popWin;
    private RecyclerView recyclerView;
    private List<PopBeans> list = new ArrayList<>();
    private ShoppingAdaoter mAdapter;
    private List<GoodsBean.DataBean> mData=new ArrayList<>();
    private PopwindPresenter popwindPresenter;

    private List<GoodsBean.DataBean> goodsBean;
    private List<PopowindBean.DataBean> mlistpopwindBean;
    private List<PopwinTwoBean.DataBean> mlistpopwindtwoBean;
    private List<PopwindThreeBean.DataBean> mlistpopwindthreeBean;
    private List<PopwindFourBean.DataBean> mlistpopwindfourBean;
    private List<ShangpinBean.DataBean> mshangpindata;
    private String id;
    private CommodityTypeAdapter adapter;

    @Override
    protected void initview(){
        popwindPresenter=new PopwindPresenter(this,this);

        FontHelper.applyFont(Shopping_Activity.this,shoppingZonghe, FontHelper.WRYHL);
        FontHelper.applyFont(Shopping_Activity.this,shoppingPrice, FontHelper.WRYHL);
        FontHelper.applyFont(Shopping_Activity.this,shoppingXiaoliang, FontHelper.WRYHL);

        //得到Marter_item_Adapter类传过来的SellerId
        id = getIntent().getStringExtra("id");
        if(id !=null){
            final MerShoppingPresenter inpo = new MerShoppingPresenter(id,this,this);
            inpo.GetDataModel();
            //recyclerview的上拉刷新
            shoppingSpview.setType(SpringView.Type.FOLLOW);
            shoppingSpview.setGive(SpringView.Give.TOP);
            shoppingSpview.setHeader(new ShoppingRefreshHead());
            shoppingSpview.setFooter(new ShoppingLoadFooter());
            shoppingSpview.setListener(new SpringView.OnFreshListener(){
                @Override
                public void onRefresh(){
                    inpo.GetDataModel();
                }
                @Override
                public void onLoadmore() {
                    inpo.GetDataModel();
                }
            });
        }
    }
    @OnClick({R.id.shopping_zonghe, R.id.shopping_price,R.id.shopping_xiaoliang})
    public void sortClick(View view){
        switch (view.getId()){
            case R.id.shopping_zonghe://综合
                mAdapter.notifyDataSetChanged();
                break;
            case R.id.shopping_price://价格
                priceSort();
                break;
            case R.id.shopping_xiaoliang://销量
                salesSort();
                break;
        }
    }
    /**
     * 按照价格排序
     */
    private void priceSort(){
        Collections.sort(mData,new Comparator<GoodsBean.DataBean>() {
            @Override
            public int compare(GoodsBean.DataBean o1, GoodsBean.DataBean o2) {

                return (int) (Float.valueOf(o2.getDiscountPrice())-Float.valueOf(o1.getDiscountPrice()));
            }
        });

        mAdapter.notifyDataSetChanged();
    }
    /**
     * 按照销量排序
     */
    private void salesSort() {
        Collections.sort(mData,new Comparator<GoodsBean.DataBean>() {
            @Override
            public int compare(GoodsBean.DataBean o1, GoodsBean.DataBean o2) {

                return (int) (Float.valueOf(o2.getGoodsSales())-Float.valueOf(o1.getGoodsSales()));
            }
        });

        mAdapter.notifyDataSetChanged();
    }

    //请求商家商品的分类列表数据
    @Override
    protected void initdata(){
        ShangpinPrestenser shangpin=new ShangpinPrestenser(this,this);
        shangpin.setId(Integer.valueOf(id));

    }
    @Override
    protected int layoutRes() {
        return R.layout.shopping_activity;
    }
    //数据返回成功
    @Override
    public void UpData(Object o){
        if (o instanceof GoodsBean){
            GoodsBean bean = (GoodsBean) o;
            goodsBean = bean.getData();
            mShoppingRecycler.setLayoutManager(new GridLayoutManager(this, 2));
            mAdapter = new ShoppingAdaoter(this);
            mAdapter.setDataGoodsBean(goodsBean);
            mShoppingRecycler.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
            shoppingSpview.onFinishFreshAndLoad();
        }else if (o instanceof PopowindBean){
            PopowindBean popowindBean= (PopowindBean) o;
            List<PopowindBean.DataBean> mlistpopwindBean = popowindBean.getData();
            mShoppingRecycler.setLayoutManager(new GridLayoutManager(this, 2));
            mAdapter = new ShoppingAdaoter(this);
            mAdapter.setDataPopowindBean(mlistpopwindBean);
        }else if (o instanceof PopwinTwoBean){
            PopwinTwoBean popowindtwoBean= (PopwinTwoBean) o;
            mlistpopwindtwoBean = popowindtwoBean.getData();
            mShoppingRecycler.setLayoutManager(new GridLayoutManager(this, 2));
            mAdapter = new ShoppingAdaoter(this);
            mAdapter.setDataPopwinTwoBean(mlistpopwindtwoBean);
        }else if (o instanceof PopwindThreeBean){
            PopwindThreeBean popowindthrerBean= (PopwindThreeBean) o;
            mlistpopwindthreeBean = popowindthrerBean.getData();
            mShoppingRecycler.setLayoutManager(new GridLayoutManager(this, 2));
            mAdapter = new ShoppingAdaoter(this);
            mAdapter.setDataPopwindThreeBean(mlistpopwindthreeBean);
        }else if(o instanceof PopwindFourBean){
            PopwindFourBean popowindfourBean= (PopwindFourBean) o;
            mlistpopwindfourBean = popowindfourBean.getData();
            mShoppingRecycler.setLayoutManager(new GridLayoutManager(this, 2));
            mAdapter = new ShoppingAdaoter(this);
            mAdapter.setDataPopwindFourBean(mlistpopwindfourBean);
        }else if(o instanceof ShangpinBean){//分类数据
            ShangpinBean shangpinBean= (ShangpinBean) o;
            mshangpindata = shangpinBean.getData();
        }
    }

    @OnClick({R.id.shopping_finsh, R.id.shopping_menu,R.id.shopping_edit})
    public void onViewClicked(View view){
        switch (view.getId()) {
            case R.id.shopping_edit:
                mShoppingEdit.setFocusableInTouchMode(true);
                break;
            case R.id.shopping_finsh:
                finish();
                break;
            case R.id.shopping_menu:
                View popupView = getLayoutInflater().inflate(R.layout.popumenu, null);
                ListView listView = (ListView)popupView.findViewById(R.id.menu_list_item);
                adapter =  new CommodityTypeAdapter(this,mshangpindata);
                listView.setAdapter(adapter);
                if (adapter == null){
                    return;
                }
                int totalHeight = 0;
                for (int i = 0, len = adapter.getCount(); i < len; i++) {
                    // listAdapter.getCount()返回数据项的数目
                    View listItem = adapter.getView(i, null, listView);
                    // 计算子项View 的宽高
                    listItem.measure(0, 0);
                    // 统计所有子项的总高度
                    totalHeight += listItem.getMeasuredHeight();
                }
                ViewGroup.LayoutParams params = listView.getLayoutParams();
                params.height = totalHeight+ (listView.getDividerHeight() * (adapter.getCount() - 1));
                listView.setLayoutParams(params);

                popWin = new PopupWindow(popupView, 420, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                //设置点击空白处消失
                popWin.setTouchable(true);
                popWin.setOutsideTouchable(true);
                popWin.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
                // 设置背景颜色变暗
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 0.4f;
                getWindow().setAttributes(lp);
                popWin.setOnDismissListener(new PopupWindow.OnDismissListener() {

                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams lp = getWindow().getAttributes();
                        lp.alpha = 1f;
                        getWindow().setAttributes(lp);
                    }
                });
                popWin.showAsDropDown(mShoppingMenu, 0, -10, Gravity.RIGHT);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        popwindPresenter.setId(Integer.valueOf(id));
                        shoppingSpview.onFinishFreshAndLoad();
                        popWin.dismiss();
                    }
                });
                break;
        }
    }
}
